<?php

class Application_Model_DbTable_Users extends Zend_Db_Table_Abstract
{

    protected $_name = 'users';
    protected $_dependentTables = array(
        'Application_Model_DbTable_UsersPasswordResets'
    );

    public function getName()
    {
        return $this->_name;
    }

    public function getPasswordResetTable()
    {
        return new Application_Model_DbTable_UsersPasswordResets();
    }

    public function insert(array $data)
    {
        parent::insert($this->_saltPassword($data));
    }

    public function update(array $data, $where)
    {
        parent::update($this->_saltPassword($data), $where);
    }

    protected function _saltPassword(array $data)
    {
        if (!empty($data['password'])) {
            $salt = $this->_getSalt();
            $data['password'] = sha1($data['password'] . $salt);
            $data['password_salt'] = $salt;
        }
        return $data;
    }

    protected function _getSalt()
    {
        $salt = '';
        for ($i = 0; $i < 50; $i++) {
            $salt .= chr(rand(33, 126));
        }
        return $salt;
    }

}

